package com.zhangpp.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangpp.dao.UserMapper;
import com.zhangpp.entity.Feedback;
import com.zhangpp.entity.User;
import com.zhangpp.service.UserService;
import com.zhangpp.vo.CommonQueryPageVO;
import com.zhangpp.vo.CommonVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;


//queryAllPage   delete   update  PageDTO  CommonVO
//CommonQueryPageVO
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    UserMapper userMapper;

    HashMap<String, Object> map = new HashMap<>();

    @Override
    public HashMap<String, Object> queryAllPage(Integer page, Integer pageSize) {
        map.put("page",page);


        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //条数
        int count = userMapper.selectCount(null);

        map.put("total",count);

        IPage<User> page1 = new Page<>(page,pageSize);

        IPage<User> users = userMapper.selectPage(page1, null);

        map.put("rows",users.getRecords());

        return map;
    }

    @Override
    public HashMap<String, Object> update(User user) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            userMapper.updateById(user);
            map.put("message","成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","失败");
        }
        return map;
    }

    @Override
    public CommonQueryPageVO queryAllPages(Integer page, Integer pageSize) {
        //总条数
        int total = userMapper.selectCount(null);

        IPage<User> page1 = new Page<>(page,pageSize);

        IPage<User> users = userMapper.selectPage(page1, null);

        return new CommonQueryPageVO(page,total,users.getRecords());
    }

    @Override
    public CommonVO updates(User user) {
        try {
            userMapper.updateById(user);
            return CommonVO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVO.faild();
        }
    }
}
